package com.fingard.dsp.bank.directbank.cmb02;

import java.io.File;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.directbank.cmb02.util.SignUtil;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

public class CMB02BatPayRec extends CMB02Base implements IBatPayRec {
	@Override
	public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
		return null;
	}

	@Override
	public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
		return null;
	}

	@Override
	public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
		RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
		retAts.reqSeqID = p_req9188.reqBatchNO;

		try {
			String tmpMacKey = "";
			String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
			if (!new File(tmpMacKeyPath).exists()) {
				String[] tmpMacKeyRet = exchangeMacKey();
				if (tmpMacKeyRet[0].length() != 0) {
					retAts.respCode = "-1";
					retAts.respInfo = tmpMacKeyRet[1];
				} else {
					tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
				}
			} else {
				tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
			}
			WriteBankLogLn("macKey:" + tmpMacKey);

			StringBuilder batchSeqNoMapping = new StringBuilder();
			
			while (p_req9188.readRecord()) {
				StringBuilder tmpEachRecordReq = new StringBuilder();

				StringBuilder tmpEachRecordBody = new StringBuilder();
				tmpEachRecordBody.append(String.format("%-20s", p_req9188.readedDetail.payAct));
				tmpEachRecordBody.append(String.format("%-20s", bankFront.corpNo));
				tmpEachRecordBody.append(String.format("%-20s", bankFront.org));
				tmpEachRecordBody.append(String.format("%-4s", ""));
				tmpEachRecordBody.append("RMB");
				tmpEachRecordBody.append(String.format("%20s", StringHelper.amountMultiply100(p_req9188.readedDetail.payAmount).replace(" ", "0")));
				tmpEachRecordBody.append(String.format("%-60s", p_req9188.readedDetail.payName));
				tmpEachRecordBody.append(String.format("%-20s", p_req9188.readedDetail.cellPhone));
				tmpEachRecordBody.append(String.format("%-3s", convertCertTypeToBank(p_req9188.readedDetail.certType)));
				tmpEachRecordBody.append(String.format("%-30s", p_req9188.readedDetail.certNum));
				tmpEachRecordBody.append(String.format("%-20s", ""));
				tmpEachRecordBody.append(String.format("%-20s", ""));
				tmpEachRecordBody.append(String.format("%-120s", ""));
				tmpEachRecordBody.append(String.format("%-32s", getBankConfig().getValueAsString("paymentBasis")));
				tmpEachRecordBody.append(String.format("%-48s", ""));
				tmpEachRecordBody.append(String.format("%-80s", ""));

				StringBuilder tmpEachRecordHeader = new StringBuilder();
				tmpEachRecordHeader.append("CMBA");
				tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
				tmpEachRecordHeader.append("CMSK");
				tmpEachRecordHeader.append(String.format("%-8s", ""));
				tmpEachRecordHeader.append(String.format("%-6s", ""));
				tmpEachRecordHeader.append(String.format("%-30s", ""));
				Date tmpDateNow = new Date();
				tmpEachRecordHeader.append(Format.Date8Format.format(tmpDateNow));
				tmpEachRecordHeader.append(Format.Time6Format.format(tmpDateNow));
				tmpEachRecordHeader.append(String.format("%-25s", p_req9188.readedDetail.askNumber));
				tmpEachRecordHeader.append("0.2.0");
				tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
				tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

				tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

				batchSeqNoMapping.append(p_req9188.readedDetail.askNumber).append("\r\n");
				
				sendToBank(tmpEachRecordReq.toString());
			}
			
			WriteBankLogLn("本机批次映射文件内容:\r\n" + batchSeqNoMapping.toString());
			String tmpLocalMarkPath = getTempFileDirectory() + File.separator + p_req9188.reqBatchNO + ".txt";
			WriteBankLogLn2("批次映射文件保存本机路径: " + tmpLocalMarkPath);
			FileHelper.writeFile(tmpLocalMarkPath, batchSeqNoMapping.toString(), getCharset());
			
			retAts.respCode = "0";
			retAts.respInfo = "批次提交成功";
			retAts.transState = "I4001";
			retAts.payInfoCode = "I4001";
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.toString();
			WriteBankLogLn(ex);
		}

		retAts.writeRetToATS();
		return retAts;
	}

	@Override
	public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
		RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req9198.reqHeader);
		retAts.reqSeqID = p_req9198.reqBatchNO;

		try {
			String tmpMacKey = "";
			String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
			if (!new File(tmpMacKeyPath).exists()) {
				String[] tmpMacKeyRet = exchangeMacKey();
				if (tmpMacKeyRet[0].length() != 0) {
					retAts.respCode = "-1";
					retAts.respInfo = tmpMacKeyRet[1];
				} else {
					tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
				}
			} else {
				tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
			}
			WriteBankLogLn("macKey:" + tmpMacKey);
			
			String tmpLocalMarkPath = getTempFileDirectory() + File.separator + p_req9198.reqBatchNO + ".txt";
			WriteBankLogLn2("批次映射文件保存本机路径: " + tmpLocalMarkPath);
			if (!new File(tmpLocalMarkPath).exists()) {
				retAts.respCode = "-1";
				retAts.respInfo = "本机批次映射文件:[" + tmpLocalMarkPath + "]不存在!";
				retAts.writeAfterDetail();
				return retAts;
			} 
			String batchSeqNoMapping = FileHelper.readFileText(tmpLocalMarkPath, getCharset());
			WriteBankLogLn("本机批次映射文件内容:\r\n" + batchSeqNoMapping);
			
			String[] tmpSeqNoArray = batchSeqNoMapping.split("\r\n");
			for (String tmpSeqNo : tmpSeqNoArray) {
				if (StringHelper.isNullOrEmpty(tmpSeqNo)) {
					continue;
				}
				StringBuilder tmpEachRecordReq = new StringBuilder();

				StringBuilder tmpEachRecordBody = new StringBuilder();
				tmpEachRecordBody.append(String.format("%-20s", ""));
				tmpEachRecordBody.append(String.format("%-20s", bankFront.corpNo));
				tmpEachRecordBody.append(String.format("%-4s", ""));
				Date tmpBgnDate = p_req9198.getBgnDateTime();
				tmpEachRecordBody.append(Format.Date8Format.format(tmpBgnDate));
				tmpEachRecordBody.append(String.format("%-30s", tmpSeqNo));

				StringBuilder tmpEachRecordHeader = new StringBuilder();
				tmpEachRecordHeader.append("CMBA");
				tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
				tmpEachRecordHeader.append("CMDB");
				tmpEachRecordHeader.append(String.format("%-8s", ""));
				tmpEachRecordHeader.append(String.format("%-6s", ""));
				tmpEachRecordHeader.append(String.format("%-30s", ""));
				
				tmpEachRecordHeader.append(Format.Date8Format.format(tmpBgnDate));
				tmpEachRecordHeader.append(Format.Time6Format.format(tmpBgnDate));
				tmpEachRecordHeader.append(String.format("%-25s", getQryTransID()));
				tmpEachRecordHeader.append("0.2.0");
				tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
				tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

				tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

				String[] tmpStrRet = sendToBank(tmpEachRecordReq.toString());
				if (tmpStrRet[0].length() == 0) {
					retAts.respCode = "0";
					
					String tmpRespCode = tmpStrRet[1].substring(0, 7);
					if ("CMBMB99".equals(tmpRespCode)) {
						RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
						
						tmpRetDetail.postScript = tmpSeqNo;
						tmpRetDetail.result = StringHelper.joinNotEmpty("+", tmpRespCode, tmpStrRet[1].substring(45, 52).trim());
						tmpRetDetail.payInfoCode = tmpStrRet[1].substring(45, 52).trim();
						tmpRetDetail.memo = tmpStrRet[1].substring(52).trim();
						
						retAts.writeRetDetail(tmpRetDetail);
					} else {
						retAts.respCode = "-1";
						retAts.respInfo = tmpRespCode + "+" + tmpStrRet[1].substring(52).trim();
					}
				} else {
					retAts.respCode = "-1";
					retAts.respInfo = tmpStrRet[1];
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeAfterDetail();
		return retAts;
	}
}
